﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Web.Script.Serialization;

namespace ALinq.BBS.DataAccess
{
    class EntityCreatedArgs : System.EventArgs
    {
        public object Entity;
    }

    partial class Post : IArticle
    {
        private Replies replies;

        partial void OnCreated()
        {
            Date = DateTime.Now;
            Type = PostType.Normal;
            LastUpdate = DateTime.Now;
            //Content = string.Empty;
        }

        //[ScriptIgnore]
        //public Post Previous
        //{
        //    get { return null; }
        //}

        [ScriptIgnore]
        public Replies Replies
        {
            get
            {
                if (replies == null)
                {
                    replies = new Replies(this);
                }
                return replies;
            }
        }

        partial void OnLoaded()
        {
            //Debug.Assert(DataContext != null);
            //DataContext.RaiseEntityCreated(this);
        }

        //partial void OnValidate(ChangeAction action)
        //{
        //    var vc = new ValidationContext(this, null, null);
        //    Validator.ValidateObject(this, vc, true);
        //}

        //// This method is called after the Post instance created by the dataContext.
        //void SetDataContext(DataContext dataContext)
        //{
        //    this.DataContext = (ArticleDataContext)dataContext;
        //}

        //internal ArticleDataContext DataContext { get; private set; }

        private Board board;
        private Reply lastReply;


        public Reply LastReply
        {
            get
            {
                if (LastReplyID == null)
                    return null;

                if (lastReply == null)
                    lastReply = DataContext.Replies.SingleOrDefault(o => o.ID == LastReplyID);

                return lastReply;
            }
        }

        public User User
        {
            get { return DataContext.Users.Single(o => o.ID == UserID); }
        }


    }
}
